{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简单线性回归教程（二）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### —— 用梯度下降法求解线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAaq0lEQVR4nO3df4xdZZ3H8feX6SAD6zoCEwJTamsk06gVihPEdGOk6BaFwKQahcguKpv+sWbXdXerbdasy0a3Nd0Nstkspos/2OBCEWrxx8ZKaI0JsZDpFq386IIgpcOP1kDZCCNMy3f/uOfS6cz9ce49v55zzueVNDP3zJ07zz3T+Z7n+T7f5znm7oiISLWcUHQDREQkfQruIiIVpOAuIlJBCu4iIhWk4C4iUkELim4AwOmnn+6LFy8uuhkiIqWye/fu37r7SKuvBRHcFy9ezOTkZNHNEBEpFTN7st3XlJYREakgBXcRkQrqGtzN7JtmdtDMfjXr2CYze8TMfmlm3zOz4VlfW29mj5nZPjNblVXDRUSkvTg9928Dl8w5djfwTnd/F/C/wHoAM3s7cCXwjuh7/t3MBlJrrYiIxNI1uLv7z4Dn5xz7ibsfiR7uAhZGn18B3Obur7j7E8BjwAUptldERGJIo1rm08CW6PNRGsG+6UB0bB4zWwOsAVi0aFEKzRARiWfbnik2bd/H04enOWt4iLWrxphY3jJUlVaiC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vEzB42s/fW8XyY2Vj0f6L57//M7K+yPhcK7r17BVjp7ucC5wGXmNmFwFeB6939bcALwLUFtjFvnwUenvW4zufiInc/b1YN8zrgHnc/B7gnelwXNwA/dvelwLk0/o/U7ny4+77o/8R5wLuBl4HvkfG5UHDvkTf8Lno4GP1zYCVwR3T8ZmCigOblzswWApcCN0WPjZqeizauoHEOoEbnwszeBLwP+AaAu7/q7oep6fmY5WLg1+7+JBmfCwX3PkRpiAeAg8DdwK+Bw+5+JHrKAaAud+D4GvB54LXo8WnU91w48BMz221ma6JjZ7j7M9HnzwJnFNO03C0BDgHfilJ2N5nZKdT3fDRdCdwafZ7puVBw74O7H42GWAuBC4ClBTepEGZ2GXDQ3XcX3ZZA/JG7nw98CPiMmb1v9he9UXdcl9rjBcD5wI3uvhx4iTlph5qdD6K5p8uB7879WhbnQsE9gWiYuRN4LzBsZs3bFi4EpgprWH5WAJeb2W+A22ikY26gnucCd5+KPh6kkVO9AHjOzM4EiD4eLK6FuToAHHD3+6LHd9AI9nU9H9C46P+Puz8XPc70XCi498jMRsxsOPp8CPggjYmincBHo6ddA9xVTAvz4+7r3X2huy+mMdzc4e6foIbnwsxOMbM3Nj8H/hj4FfB9GucAanIuANz9WeApMxuLDl0MPERNz0fkKo6lZCDjc6EVqj0ys3fRmPwYoHFxvN3d/9HM3kqj93oqsAe42t1fKa6l+TKz9wN/6+6X1fFcRO/5e9HDBcB/uftXzOw04HZgEY1trT/m7s8X1Mxcmdl5NCbaTwQeBz5F9DdDzc5HdMHfD7zV3V+MjmX6f0PBXUSkgpSWERGpIAV3EZEKUnAXEakgBXcRkQpScBcRqSAFdxGRClJwFxGpoP8H1k8f4K2JgJEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "points = numpy.genfromtxt('data.csv', delimiter=',')\n",
    "N = len(points)\n",
    "\n",
    "# 提取 x，y\n",
    "x = numpy.array(points[:,0])\n",
    "y = numpy.array(points[:,1])\n",
    "\n",
    "# 用plt做出散点图\n",
    "plt.scatter(x,y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义线性模型 y = mx + b\n",
    "\n",
    "def compute_cost(points, b, m):\n",
    "    total_cost = 0\n",
    "    \n",
    "    N = len(points)\n",
    "    \n",
    "    # 计算所有误差平方的和 sum(y-mx-b)^2\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        \n",
    "        total_cost += (y - m * x - b) ** 2\n",
    "    \n",
    "    return total_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 定义模型的超参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 包括：步长（学习速率），初始点，迭代次数\n",
    "# 步长或者学习率\n",
    "learning_rate = 0.0000002\n",
    "# learning_rate = 10\n",
    "# b的初始值\n",
    "initial_b = 0\n",
    "# m的初始值\n",
    "initial_m = 0\n",
    "# 迭代次数\n",
    "num_iteration = 30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 定义梯度下降算法函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradient_descent(points, initial_b, initial_m, learning_rate, num_iteration):\n",
    "    b = initial_b\n",
    "    m = initial_m\n",
    "    \n",
    "    # 用一个list记录所有的损失函数值\n",
    "    cost_list = []\n",
    "    \n",
    "    for i in range(num_iteration):\n",
    "        cost_list.append(compute_cost(points, b, m))\n",
    "        b, m = step_grad_desc(b, m, array(points), learning_rate)\n",
    "    \n",
    "    return [b, m, cost_list]\n",
    "\n",
    "# current_b为当前轮的b的值\n",
    "# current_m为当前轮的m的值\n",
    "# points为训练数据\n",
    "# learning_rate为步长\n",
    "def step_grad_desc(current_b, current_m, points, learning_rate):\n",
    "    m_grad = 0\n",
    "    b_grad = 0\n",
    "    \n",
    "    N = len(points)\n",
    "    \n",
    "    for i in range(N):\n",
    "        # (x,y)为第i个数据点的坐标\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        m_grad += (current_m * x + current_b - y) * x\n",
    "        b_grad += current_m * x + current_b - y\n",
    "    \n",
    "    m_update = current_m - learning_rate * m_grad * 2\n",
    "    b_update = current_b - learning_rate * b_grad * 2\n",
    "    \n",
    "    return b_update, m_update"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 测试：运行梯度下降函数来计算最优m和b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "final m:  1.4155418842569853\n",
      "final b:  0.028182744497979542\n"
     ]
    }
   ],
   "source": [
    "b, m , cost_list = gradient_descent(points, initial_b, initial_m, learning_rate, num_iteration)\n",
    "\n",
    "print(\"final m: \", m)\n",
    "print(\"final b: \", b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "final cost:  122.66184855455226\n"
     ]
    }
   ],
   "source": [
    "print(\"final cost: \", compute_cost(points,b,m))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. 画出损失函数随着迭代下降的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD4CAYAAADrRI2NAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZX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AUcBP6vv+WcOTPLBp4Evuica+u/Lh6/p1PsT1x/R865sHOuiujzTOYDM870PRIx+BPyYS/OuY8u464AAAFVSURBVHpv2gD8mugXHu8Oe/2wff2xDT7Xc86cc4e9/zEjwI+Js+/J6zd+Evgv59xTXnPcfk+n2p94/476OOdagNXAh4A8M+u72/KgmZeIwZ9wD3sxsyzv4hRmlgUsBt46/aviwgpgqTe/FHjGx1qGRV9Aej5GHH1P3oXDh4Ftzrlv9VsVl9/TQPsT599RkZnlefOZRAexbCN6APikt9mg31HCjeoB8IZnfYd3H/byNZ9LOidmNoXoWT5En6Hw83jbJzP7BfBhoreQPQzcDzwN/BKYTPT22zc55+LmYukA+/Rhol0IDtgDfK5f/3hMM7MFwB+BzUDEa/4Hov3icfc9nWZ/PkX8fkcXEL14GyR64v5L59y/ehnxGFAAvAnc5pzrHvB9EjH4RURkYInY1SMiIqeh4BcRSTIKfhGRJKPgFxFJMgp+EZEko+AXEUkyCn4RkSTz/wG+AkVgtz0B8QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(cost_list)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "迭代5次之后，就达到了极小值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. 画出拟合曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dfZBdVZ3u8e8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mMC8G7nfOnQjcX3jcLK4H7nXOTQNOxvs/0nT7wzm3tfB/YgZwCrAH+DkN3hcK9yo5z18KD1sLfxxwBnBnYfstQHcCzXzzSaYAAAIwSURBVIudmXUC5wA/LDw2mnRfBDgPbx9AE+0LMzsCeC/wIwDn3F7n3G6adH+McCbwtHNuOw3eFwr3GhTKEI8AO4H7gKeB3c65fYWn9APNcgeO64DLgf2Fx0fRvPvCAb8xs01mtqCw7Rjn3POFf78AHJNM02I3BdgF/LhQsvuhmR1O8+6PoguA2wv/bui+ULjXwDk3XDjF6gROBaYl3KREmNlcYKdzblPSbUmJdzvnZgFnA18ws/eO/KLz5h03y9zj8cAs4Ebn3EzgNUrKDk22PyiMPZ0L/Kz0a43YFwr3OhROM9cB7wLazax428JOYCCxhsVnNnCumW0DfopXjrme5twXOOcGCn/vxKupngq8aGbHAhT+3plcC2PVD/Q75x4oPL4TL+ybdX+Ad9B/2Dn3YuFxQ/eFwr1KZjbRzNoL/24DPoA3ULQO+GjhafOBu5NpYXyccz3OuU7n3GS80821zrkLacJ9YWaHm9mbiv8G/gF4DLgHbx9Ak+wLAOfcC8CzZja1sOlM4I806f4o+AQHSzLQ4H2hK1SrZGZ/hzf40YJ3cFzpnPu/ZnYCXu/1SKAP+JRz7o3kWhovM3sf8H+cc3ObcV8UvuefFx6OB/7dOXe1mR0FrAQm4S1rfb5z7uWEmhkrM5uBN9B+CPAMcBGF3xmabH8UDvg7gBOcc68WtjX0/4bCXUQkh1SWERHJIYW7iEgOKdxFRHJI4S4ikkMKdxGRHFK4i4jkkMJdRCSH/j8cWDnud76OMgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "\n",
    "# 针对每一个x，求出预测值\n",
    "y_pred = m * x + b\n",
    "\n",
    "plt.plot(x, y_pred, c='r')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
